package HttpPages;

import java.util.logging.Level;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

import Database.Database;
import Logger.AppLogger;
import Objects.ServerClientCodes;
import Objects.UserProfile;


@Path("/register")
public class Register
{

	//-----------------------------------http methods--------------------------------

	// This method is called if TEXT_PLAIN is request
	@POST
	@Consumes(MediaType.TEXT_PLAIN)
	public String registerationRequest(@FormParam("android_id") String android_id, 
			@FormParam("google_account") String google_account,
			@FormParam("c2dm_register_id") String c2dm_register_id)
	{
		AppLogger.setLevel(Level.INFO);
		AppLogger.write(Level.INFO, "Registration Request was recieved!");

		AppLogger.write(Level.INFO, "android_id: " + android_id + " google_account: " + 
				google_account + " c2dm_register_id: " + c2dm_register_id);

		//if user(android_id) already in db, update his c2dm_register_id & google_account
		if (Database.getInstance().getUserProfile(android_id) != null){
			AppLogger.write(Level.INFO, "android_id: " + android_id + " already in DB, updating c2dm_register_id & google_account");
			Database.getInstance().updateUserDetails(android_id,c2dm_register_id, google_account);
		}
		else{
			//adding the userProfile to the database
			AppLogger.write(Level.INFO, "adding new User to DB. (android_id = " + android_id + " )");
			UserProfile newUser = new UserProfile(android_id, google_account, c2dm_register_id);
			Database.getInstance().addUserProfile(newUser);
		}
/*
		//sending a testing c2dm message back to the client
		C2DMSender sender = new C2DMSender();
		sender.sendC2DMMessage("testing", c2dm_register_id, "testing works!!!!!!!!!!?");
*/		
		//TODO  -  need to create Response codes file and handle everything!!! 
		return ServerClientCodes.C2DMRegistrationKeyReceived;
	}

}
